package com.weixin.demo.service;

import com.weixin.demo.core.utils.SHAUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;

/**
 * 测试问卷星登录接口
 */
@Service
public class LoginService {

    private static final String appId = "1254226";

    private static final String appKey = "7e25f198a4704cbe8f5de3b98c9af72c";

    @Resource
    private RestTemplate restTemplate;

    /**
     * 获取sign
     * sign的计算方法
     * sign = sha1(appid+appkey+username+mobile+subuser+ts)
     *
     * appid+appkey+subuser+mobile+email+roleId+ts
     */
    public static String getSign(String subuser, String mobile, String email,String roleId, String ts) throws NoSuchAlgorithmException {

        String joinStr = appId + appKey + subuser + mobile + email + roleId + ts;
//        return SHAUtils.sha1(joinStr);
        return SHAUtils.getSha1(joinStr);
    }

    public String testLogin() throws Exception {

        String username = "刘先生";
        String mobile = "18001062635";
        String email = "18001062635@163.com";

        String roleId = "2";

        long timestamp = System.currentTimeMillis() / 1000;
        String ts = String.valueOf(timestamp);

        String subuser = "liu123456";
//        String subuser = "liu000";

        String sign = getSign(subuser, mobile, email,roleId, ts);

//        String url = "https://www.wjx.cn/partner/login.aspx?appid="+ appId +"&username="+username+"&mobile="+mobile+"&subuser="+subuser+"&ts="+ts+"&sign="+sign;

//        String url = "https://www.wjx.cn/zunxiang/login.aspx?appid=" + appId + "&subuser=" + subuser + "&mobile=" + mobile + "&email=" + email + "&roleId=" + roleId + "&ts=" + ts + "&sign="+sign;
        String url = "https://survey.wegrowx.com/zunxiang/login.aspx?appid=" + appId + "&subuser=" + subuser + "&mobile=" + mobile + "&email=" + email + "&roleId=" + roleId + "&ts=" + ts + "&sign="+sign;

        String result =  restTemplate.getForObject(url, String.class);

        return result;
    }

}
